package com.xc.hotelmgr.webapi.modules.ord.controller;

import java.util.*;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.xc.hotelmgr.webapi.common.ali.msg.Sample;
import com.xc.hotelmgr.webapi.common.utils.PageUtils;
import com.xc.hotelmgr.webapi.common.utils.R;
import com.xc.hotelmgr.webapi.modules.ht.service.HtHotelinfoService;
import com.xc.hotelmgr.webapi.modules.ht.service.HtPhysicalRoomService;
import com.xc.hotelmgr.webapi.modules.ord.dao.OrdGuestDao;
import com.xc.hotelmgr.webapi.modules.ord.dao.OrdInvoiceInfoDao;
import com.xc.hotelmgr.webapi.modules.ord.entity.*;
import com.xc.hotelmgr.webapi.modules.ord.service.*;
import com.xc.hotelmgr.webapi.modules.sys.controller.AbstractController;
import lombok.RequiredArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;


/**
 * 订单信息
 */
@RestController
@RequestMapping("ord/order")
@RequiredArgsConstructor
public class OrdOrderController extends AbstractController {

    private final OrdOrderVOService ordOrderVOService;

    private final OrdWorkOrderService ordWorkOrderService;

    private final OrdGuestDao ordGuestDao;

    private final OrdInvoiceInfoDao ordInvoiceInfoDao;

    private final OrderDealService orderDealService;

    private final OrdOrderService ordOrderService;

    private final HtHotelinfoService htHotelinfoService;

    private final HtPhysicalRoomService htPhysicalRoomService;


    /**
     * 订单列表
     * @param params
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @RequiresPermissions("ord:order:list")
    public R list(@RequestBody Map<String, Object> params){
        Integer dtId = getUserDtId();
        params.put("dtId", dtId);
        params.put("sidx", "id");
        params.put("ord", "desc");
        PageUtils page = ordOrderVOService.sqlQuryPage(params);
        return R.ok().put("page", page).put("me", getUserId());
    }

    /**
     * 入住人信息
     * @param params
     * @return
     */
    @RequestMapping("/peo")
    @RequiresPermissions("ord:order:peo")
    public R peo(@RequestParam Map<String, Object> params){
        Long orderCode = Long.parseLong((String) params.get("orderCode"));
        List<OrdGuestEntity> ordGuestEntities = ordGuestDao.getByOrderCode(orderCode);
        return R.ok().put("peo", ordGuestEntities);
    }

    /**
     * 发票信息
     * @param params
     * @return
     */
    @RequestMapping("/invo")
    @RequiresPermissions("ord:order:invo")
    public R invo(@RequestParam Map<String, Object> params){
        Long orderCode = Long.parseLong((String) params.get("ordCode"));
        OrdInvoiceInfoEntity ordInvoiceInfoEntity = ordInvoiceInfoDao.getByOrdCode(orderCode);
        return R.ok().put("invo_info", ordInvoiceInfoEntity);
    }

    /**
     * 查看备注
     * @param params
     * @return
     */
    @RequestMapping("/getnote")
    //@RequiresPermissions("ord:order:getnote")
    public R getnote(@RequestParam Map<String, Object> params){
        Long orderCode = Long.parseLong((String) params.get("id"));
        QueryWrapper<OrdWorkOrderEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id", orderCode);
        List<OrdWorkOrderEntity> ordWorkOrderEntities = ordWorkOrderService.getBaseMapper().selectList(wrapper);
        return R.ok().put("notes", ordWorkOrderEntities);
    }

    /**
     * 设置备注
     * @param params
     * @return
     */
    @RequestMapping("/setnote")
    //@RequiresPermissions("ord:order:setnote")
    public R setnote(@RequestParam Map<String, Object> params){
        Long orderCode = Long.parseLong((String) params.get("id"));
        String text = (String) params.get("info");
        String name = getUser().getUsername();
        OrdWorkOrderEntity ordWorkOrderEntity = new OrdWorkOrderEntity();
        ordWorkOrderEntity.setOrderId(orderCode);
        ordWorkOrderEntity.setName(name);
        ordWorkOrderEntity.setInfo(text);
        ordWorkOrderEntity.setTime(new Date());
        ordWorkOrderService.getBaseMapper().insert(ordWorkOrderEntity);
        return R.ok();
    }

    /**
     * 跟单 （订单状态不为1操作员可以进行跟单）
     * @param params
     * @return
     */
    @RequestMapping("lock")
    @RequiresPermissions("ord:order:lock")
    public R lock(@RequestParam Map<String, Object> params) {

        Integer userId = Math.toIntExact(getUserId());
        Long id = Long.parseLong((String) params.get("id"));
        String username = getUser().getUsername();
        return orderDealService.getLock(userId, id, username);
    }

    /**
     * 订单处理
     * @param params
     * @return
     */
    @RequestMapping("deal")
    @RequiresPermissions("ord:order:deal")
    public R deal(@RequestParam Map<String, Object> params) {
        Long userId = getUserId();
        Long dealOrderPeo = Long.valueOf((String)params.get("dealOrderPeo"));
        if(!dealOrderPeo.equals(userId)) {                      // 跟单后，其他跟单人不能处理订单
            return R.error(-1, "跟单人不符");
        }
        return R.ok();
    }

    /**
     * 多数据源价格日历
     * @param params
     * @return
     */
    @RequestMapping("rateslist")
    //@RequiresPermissions("ord:order:rateslist")
    public R rateslist(@RequestParam Map<String, Object> params) {
        Integer id = Integer.valueOf((String) params.get("id")); // 订单id
        List<Map<String, Object>> list = orderDealService.getRatesList(id);
        return R.ok().put("list", list);
    }

    /**
     * 下单
     * @param params
     * @return
     */
    @RequestMapping("buy")
    //@RequiresPermissions("ord:order:buy")
    public R buy(@RequestParam Map<String, Object> params) {
        Long id = Long.valueOf((String)params.get("id"));
        Long rpCode = Long.valueOf((String)params.get("rpCode"));
        Long userId = getUserId();
        return orderDealService.buy(id, rpCode, userId);
    }

    /**
     * 自采订单
     * @param params
     * @return
     */
    @RequestMapping("buyother")
    //@RequiresPermissions("ord:order:buyother")
    public R buyother(@RequestParam Map<String, Object> params) {
        Long id = Long.valueOf((String)params.get("id"));
        Long userId = getUserId();
        Integer optType = Integer.valueOf((String)params.get("optType"));
        Integer status = Integer.valueOf((String)params.get("status")); // 下单渠道
        Integer price = Integer.valueOf((String)params.get("price"));
        String orderCodeOther = (String)params.get("orderCode");
        return orderDealService.buyOther(id, userId, optType, status, price, orderCodeOther);
    }

    /**
     * 关单/确认无房
     * @param params
     * @return
     */
    @RequestMapping("refuse")
    //@RequiresPermissions("ord:order:refuse")
    public R refuse(@RequestParam Map<String, Object> params) {
        Long id = Long.valueOf((String)params.get("id"));
        Integer optType = Integer.valueOf((String)params.get("optType"));
        Long userId = getUserId();
        return orderDealService.refuse(id, optType, userId);
    }

    /**
     * 价格价格政策列表
     * @param params
     * @return
     */
    @RequestMapping("pslist")
    @RequiresPermissions("ord:order:pslist")
    public R pslist(@RequestParam Map<String, Object> params) {
        Long userId = getUserId();
        int buyStatus = Integer.parseInt((String) params.get("buyStatus"));
        Long dealOrderPeo = Long.valueOf((String)params.get("dealOrderPeo"));
        if(buyStatus == 0) {                                    // 跟单状态
            return R.error(-1, "未跟单");
        }
        if(!dealOrderPeo.equals(userId)) {                      // 跟单后，其他跟单人不能处理订单
            return R.error(-1, "跟单人不符");
        }
        return R.ok();
    }

    /**
     * 短信发送业务
     * @param params
     * @return
     */
    @RequestMapping("sendmsg")
    public R sendMsg(@RequestParam Map<String, Object> params) {
//        Integer id = Integer.valueOf(params.get("id").toString());
//        OrdOrderEntity ordOrderEntity = ordOrderService.getById(id);
//
//        String phone = ordOrderEntity.getPhone();   // 联系人电话
//        String name = ordOrderEntity.getName();     // 联系人姓名
//
//        String accessKeyId = "LTAI5tPhK5KFvb7QuD1YGAVm";
//        String accessKeySecret = "YYcIXYVwik4nhCOvh4MjeuspS2T486";     // 密钥
//
//        String templateCode = "SMS_213286944";      // 模板编码
//        String templateSignName = "行程旅游";        // 短信模板签名
//
//        JSONObject jsonObject = new JSONObject();     // 短信json串
//        jsonObject.put("name", name);
//        SendSmsRequest sendSmsRequest = new SendSmsRequest();
//        sendSmsRequest.setPhoneNumbers(phone);
//        sendSmsRequest.setSignName(templateSignName);
//        sendSmsRequest.setTemplateCode(templateCode);
//        sendSmsRequest.setTemplateParam(jsonObject.toString());  // json串
//        Sample sample = new Sample();
//        R r = sample.sendMsg(accessKeyId, accessKeySecret, sendSmsRequest);
//        if(Integer.parseInt(r.get("code").toString()) == 0) {
//            ordOrderEntity.setIsMsg(1);
//            ordOrderService.updateById(ordOrderEntity);
//            return r;
//        }
        return R.error("短信发送失败");
    }
}
